﻿#include <stdio.h>
#include <math.h>

//计算一个整数的阶乘
float ComputeFactorial(int IntInput);
//求最大公约数
/* m mod n = 商s × n ... 余数r，如果r等于0，则对大公约数为n
  如果r不等于0，n赋给m，r赋给n，继续算余数，直到余数为0
 */
int GCD(int int1, int int2);
//判断一个整数是不是质数
//返回值，1：是质数，0：不是质数
int IsPrime(int IntInput);

//打印名字和学号的函数
void PrintInfo(void)
{
	printf("My Name is : %s\n", "XYZ");
	printf("My Student ID is : %s\n", "123456789");
}
//计算一个整数的阶乘
float ComputeFactorial(int IntInput)
{
	float result;
	result = 1;
	while (IntInput > 1)
	{
		result = result * IntInput;
		IntInput--;
	}
	return result;
}
//判断一个整数是不是质数
//返回值，1：是质数，0：不是质数
int IsPrime(int IntInput)
{
	int result;
	result = 1;
	IntInput = abs(IntInput);
	int LoopNumber;
	LoopNumber = round(sqrt(IntInput)) + 1;
	for (int i = 2; i <= LoopNumber; i++)
	{
		if (IntInput % i == 0)
		{
			result = 0;
		}
	}
	return result;
}

//求最大公约数
/* m mod n = 商s × n ... 余数r，如果r等于0，则对大公约数为n
如果r不等于0，n赋给m，r赋给n，继续算余数，直到余数为0
*/
int GCD(int int1, int int2)
{
	int m, n, r;
	if (int1 > int2)
	{
		m = int1;
		n = int2;
	}
	else
	{
		m = int2;
		n = int1;
	}
	r = m % n;

	while (r != 0)
	{
		m = n;
		n = r;
		r = m % n;
	}
	return n;
}
int main()
{
	PrintInfo();
	// input from keyboard
	char InputChar;
	int ANumber, Number1, Number2;
	int IntResult;
	double DResult;
	do
	{
		printf(">Please input a char(like F for factorial operation,P for prime number checking,G for GCD,E for exit)\n");
		fflush(stdin); // Clear the cache
		InputChar = getchar();
		printf("Your input is %c(Code: %d)\n", InputChar, InputChar);
		switch (InputChar)
		{
		case 'F':
			printf(">Please input an integer\n");
			scanf("%d", &ANumber);
			printf("The number inputted is %d\n", ANumber);
			DResult = ComputeFactorial(ANumber);
			printf("The factorial of %d is %.0f\n", ANumber, DResult);
			break;

		case 'P':
			printf("Please input an integer\n");
			scanf("%d", &ANumber);
			printf("The number inputted is %d\n", ANumber);
			IntResult = IsPrime(ANumber);
			if (IntResult == 1)
			{
				printf("%d is a prime numer\n", ANumber);
			}
			else
			{
				printf("%d is not a prime numer\n", ANumber);
			}
			break;

		case 'G':
			printf("Please input two integers\n");
			scanf("%d%d", &Number1, &Number2);
			printf("The numbers inputted is %d and %d\n", Number1, Number2);
			IntResult = GCD(Number1, Number2);
			printf("%d and %d has greatest common divisor %d\n", Number1, Number2, IntResult);
			break;

		default:
			break;
		}

	} while (InputChar != 'E');
}
